package pl.kosmowska.ewelina.client.views.impl;

import java.util.Date;
import java.util.List;

import pl.kosmowska.ewelina.client.ViewMessages;
import pl.kosmowska.ewelina.client.app.requestfactory.dto.StockDataProxy;
import pl.kosmowska.ewelina.client.views.StockEditView;
import pl.kosmowska.ewelina.client.views.widgets.MessageDialogBox;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DoubleBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;

public class StockEditViewImpl extends Composite implements StockEditView{

	private static StockEditViewImplUiBinder uiBinder = GWT
			.create(StockEditViewImplUiBinder.class);
	@UiField Button confirmButton;
	@UiField HorizontalPanel datePanel;
	@UiField ListBox nameBox;
	@UiField DoubleBox valueBox;
	@UiField DoubleBox stockNumberBox;


	DateBox startDateBox;

	private Presenter presenter;
	private boolean alreadyInit;
	private StockDataProxy stockData;


	interface StockEditViewImplUiBinder extends UiBinder<Widget, StockEditViewImpl> {
	}

	public StockEditViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
		initView();
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}

	private void initView() {
		datePanel.add(initStartDateBox());
	}

	@Override
	public void reInitView() {
		if(stockData!=null){
			fillForm();
		}else{
			clearView();
		}
	}

	private void clearView() {
		startDateBox.setValue(null);
		nameBox.setSelectedIndex(-1);
		valueBox.setValue(null);
		stockNumberBox.setValue(null);
		
	}

	@Override
	public void setData(StockDataProxy StockData) {
		this.stockData = StockData;
	}

	private void fillForm() {
		valueBox.setValue((double)stockData.getStartValue());
		stockNumberBox.setValue((double)stockData.getStockNumber());
		startDateBox.setValue(stockData.getStartDate());
		fillNameBox();
	}

	private void fillNameBox(){
		if(stockData!=null){
			for(int i=0; i<nameBox.getItemCount(); i++){
				if (nameBox.getValue(i).equals(stockData.getName())){
					nameBox.setSelectedIndex(i);
					break;
				}
			}
		}else{
			nameBox.setSelectedIndex(0);	
		}

	}
	public HorizontalPanel initStartDateBox(){
		// Create a DateBox
		DateTimeFormat dateFormat = DateTimeFormat.getLongDateFormat();
		startDateBox = new DateBox();
		startDateBox.setFormat(new DateBox.DefaultFormat(dateFormat));

		// Combine the widgets into a panel and return them
		HorizontalPanel vPanel = new HorizontalPanel();
		vPanel.add(new HTML("Data pierwszej raty: "));
		vPanel.add(startDateBox);
		return vPanel;
	}


	private void saveStockData(String name, float value, int stockNumber, Date startDate) {
		presenter.saveStockData(name, value, stockNumber, startDate);


	}

	@UiHandler("returnButton")
	void onReturnButtonClick(ClickEvent event) {
		presenter.goToStockPlace();
	}


	@UiHandler("confirmButton")
	void onConfirmButtonClick(ClickEvent event) {
		if(validateForm()){
			String name = nameBox.getValue(nameBox.getSelectedIndex());
			float value = valueBox.getValue().floatValue();
			int stockNumber = stockNumberBox.getValue().intValue();
			Date startDate =startDateBox.getValue();
			saveStockData(name,value,stockNumber,startDate);
		}else{
			showMessage(ViewMessages.ERROR_WRONG_INPUT);
		}
	}

	private boolean validateForm() {
		if(!validate(valueBox) || !validate(startDateBox)){
			return false;
		}else{
			return true;
		}
	}


	private boolean validate(DateBox dateBox) {
		if(dateBox.getValue()==null)
			return false;
		else
			return true;
	}

	private boolean validate(DoubleBox doubleBox) {
		if(doubleBox.getValue()==null)
			return false;
		else
			return true;
	}


	float round (float number){
		NumberFormat newFormat =NumberFormat.getFormat("#.##");
		return  Float.valueOf(newFormat.format(number));
	}

	@Override
	public void fillStockNameList(List<String> stockList) {
		nameBox.clear();
		for(String stockName:stockList){
			nameBox.addItem(stockName);
		}
		fillNameBox();

	}

	@Override
	public void showMessage(String errorMessage) {
		MessageDialogBox messageDialog = new MessageDialogBox(errorMessage);
		messageDialog.center();
		messageDialog.show();

	}
	@Override
	public boolean isAlreadyInit() {
		return alreadyInit;
	}
	@Override
	public void setAlreadyInit(boolean alreadyInit) {
		this.alreadyInit = alreadyInit;
	}




}
